<!DOCTYPE html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        WebChat.renderWebChat(
          {
            dir: 'rtl',
            directLine: WebChat.createDirectLine({ token: await testHelpers.token.fetchDirectLineToken() }),
            store: testHelpers.createStore()
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();
        await pageObjects.sendMessageViaSendBox('carousel');
        await pageConditions.numActivitiesShown(2);

        // GIVEN: Carousel is at left most position.
        const carouselLayout = document.querySelector('.webchat__carousel-layout');
        const carouselFilmstrip = carouselLayout.querySelector('.webchat__carousel-filmstrip');

        expect(carouselFilmstrip.scrollLeft).toBe(0);

        // WHEN: Left flipper is clicked. In RTL, the left flipper goes next.
        const leftFlipper = carouselLayout.querySelector('[aria-label="Next"]');

        // Improve test reliability by hover before click on flipper button.
        await host.hover(leftFlipper);
        await testHelpers.sleep(500);
        await host.click(leftFlipper);

        // THEN: It should scroll left.
        await pageConditions.became('Carousel should scroll left', () => carouselFilmstrip.scrollLeft < -100, 1000);

        await testHelpers.sleep(500); // Wait both flippers to fade in.
        await host.snapshot('local');

        // WHEN: Right flipper is clicked. In RTL, the right flipper goes back.
        const rightFlipper = carouselLayout.querySelector('[aria-label="Previous"]');

        // Improve test reliability by hover before click on flipper button.
        await host.hover(rightFlipper);
        await testHelpers.sleep(500);
        await host.click(rightFlipper);

        // THEN: It should scroll right.
        await pageConditions.became(
          'Carousel should scroll right',
          () => Math.abs(carouselFilmstrip.scrollLeft) < 1,
          1000
        ); // 1 pixel for tolerance.

        await testHelpers.sleep(500); // Wait for right flipper to fade away.
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
